package com.jrx.anytxn.param.service.product.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.product.PrDelqTableReq;
import com.jrx.anytxn.param.dto.product.PrDelqTableRes;
import com.jrx.anytxn.param.entity.PrDelqTable;
import com.jrx.anytxn.param.mapper.ext.ExtPrDeqlTableMapper;
import com.jrx.anytxn.param.service.product.IDelqTableService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 延滞参数实现类
 *
 * @author yf
 * date 2019/10/17 0017
 * @version V1.0
 **/
@Service
@CacheConfig(cacheNames = "DELQ")
public class DelqServiceImpl implements IDelqTableService {

    private static final Logger logger = LoggerFactory.getLogger(DelqServiceImpl.class);
    @Resource
    private ExtPrDeqlTableMapper extPrDeqlTableMapper;

    @Autowired
    private ISegmentService segmentService;


    @Override
    public PrDelqTableRes findByTenIdAndDelqId(String tenantId, String delqId) throws TxnBizException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(delqId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrDelqTable delqTable = extPrDeqlTableMapper.selectByTenIdAndDelqId(tenantId, delqId);
        return BeanMapping.copy(delqTable, PrDelqTableRes.class);
    }

    @Override
    public TxnPage<PrDelqTableRes> findAllPageByTenId(Integer pageNum, Integer pageSize, String tenantId) throws TxnBizException {
        Page<PrDelqTableRes> page = PageHelper.startPage(pageNum, pageSize, Constant.ORDER_BY_SORT);
        List<PrDelqTable> prDelqTableList = extPrDeqlTableMapper.selectByTenId(tenantId);
        if (prDelqTableList.isEmpty()) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnBizException(TxnRespCode.NO_DATA.getMsg());
        }
        List<PrDelqTableRes> res = BeanMapping.copyList(prDelqTableList, PrDelqTableRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public PrDelqTableRes addDelqTable(PrDelqTableReq prDelqTableReq) throws TxnBizException {
        PrDelqTable delqTable = extPrDeqlTableMapper.selectByTenIdAndDelqId(prDelqTableReq.getTenantId(), prDelqTableReq.getDelqId());
        if (delqTable != null) {
            logger.error(TxnRespCode.DATA_EXISTS.getMsg());
            throw new TxnBizException(TxnRespCode.DATA_EXISTS.getMsg());
        }
        PrDelqTable prDelqTable = BeanMapping.copy(prDelqTableReq, PrDelqTable.class);
        try {
            prDelqTable.setId(segmentService.getId(Constant.PR_DELQ_TABLE));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnBizException(ErrorMsgConstant.ERROR_ID);
        }
        prDelqTable.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPrDeqlTableMapper.insertSelective(prDelqTable);
        return BeanMapping.copy(prDelqTable, PrDelqTableRes.class);
    }

    @Caching(evict={@CacheEvict(key="'delq_'+#prDelqTableReq.tenantId+'_'+#prDelqTableReq.delqId"),
            @CacheEvict( key="'delqParam_" + "'+#prDelqTableReq.tenantId+'_'+#prDelqTableReq.delqId")})
    @Override
    public PrDelqTableRes modifyDelqTable(PrDelqTableReq prDelqTableReq) throws TxnBizException {
        PrDelqTable prDelqTable = extPrDeqlTableMapper.selectByTenIdAndDelqId(prDelqTableReq.getTenantId(), prDelqTableReq.getDelqId());
        if (prDelqTable == null) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnBizException(TxnRespCode.NO_DATA.getMsg());
        }
        PrDelqTable delqTable = BeanMapping.copy(prDelqTableReq, PrDelqTable.class);
        // 更新
        extPrDeqlTableMapper.updateByTenIdAndDelqId(delqTable);
        return BeanMapping.copy(delqTable, PrDelqTableRes.class);
    }

    @Caching(evict={@CacheEvict(key="'delq_'+#tenantId+'_'+#delqId"),
            @CacheEvict( key="'delqParam_" + "'+#tenantId+'_'+#delqId")})
    @Override
    public Boolean removeByTenIdAndDelqId(String tenantId, String delqId) throws TxnBizException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(delqId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int i = extPrDeqlTableMapper.deleteByTenIdAndDelqId(tenantId, delqId);
        return i > 0;
    }

    @Cacheable(key="'delq_'+#tenantId+'_'+#delqId")
    @Override
    public PrDelqTable findByDelqId(String tenantId, String delqId) throws TxnBizException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(delqId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnBizException(TxnRespCode.BIZ_ERROR.getCode(),TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrDelqTable delqTable = extPrDeqlTableMapper.selectByTenIdAndDelqId(tenantId, delqId);
        if(delqTable == null){
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnBizException(TxnRespCode.BIZ_ERROR.getCode(),TxnRespCode.NO_DATA.getMsg());
        }
        return delqTable;
    }
}
